﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ADService;
using System.Web.Security;
using System.IO;
using Avatar.Common;

public partial class Company_JobAD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Membership.GetUser() != null)
            {
                var _ad = ADManager.GetAD(Membership.GetUser().UserName);
                GridView1.DataSource = _ad;
                GridView1.DataBind();
            }
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(txt_ID.Value) ? Guid.Empty : new Guid(txt_ID.Value);
            string _user = Membership.GetUser().UserName;
            Guid _adCategory = string.IsNullOrEmpty(jobADTypeList.SelectedValue) ? Guid.Empty : new Guid(jobADTypeList.SelectedValue);

            #region 入口检测

            if (!FileUpload1.HasFile) throw new Exception("*请选择要上传的文件");

            Boolean _fileOK = false;
            String _fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] _allowedExtensions = { ".gif", ".jpeg", ".jpg" };
            foreach (var _e in _allowedExtensions)
            {
                if (_fileExtension.ToLower() == _e.ToLower())
                {
                    _fileOK = true;
                }
            }
            if (_fileOK != true) throw new Exception("*文件类型错误：文件只能是扩展名为:gif、jpeg、jpg的文件.");

            #endregion

            var _year = DateTime.Now.Year.ToString();
            var _month = DateTime.Today.Month.ToString();
            var _day = DateTime.Today.Day.ToString();
            var _hour = DateTime.Now.Hour.ToString();
            var _minute = DateTime.Now.Minute.ToString();
            var _second = DateTime.Now.Second.ToString();

            string _path = "~/ad/" + _year + "/" + _month + "/" + _day + "/" + _hour + "/";
            String _savePath = Server.MapPath("~/ad/") + _year + "\\" + _month + "\\" + _day + "\\" + _hour + "\\";
            Directory.CreateDirectory(_savePath);

            string _fileName = Guid.NewGuid().ToString() + _fileExtension;

            string _iconURL = _savePath + _fileName;


            FileUpload1.PostedFile.SaveAs(_iconURL);
            ADManager.BuyAD(txt_Title.Text, _id, _user, _adCategory, _path + _fileName);

            Helper.ShowMassage("广告发布成功！", Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
